package com.chaodev.base.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.dao.AttDao;
import com.chaodev.base.dao.ExplanationPointDao;
import com.chaodev.base.dto.ExplanationPointDto;
import com.chaodev.base.em.BizTypeEnum;
import com.chaodev.base.model.Att;
import com.chaodev.base.model.ExplanationLine;
import com.chaodev.base.model.ExplanationPoint;
import com.chaodev.base.service.ExplanationPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ExplanationPointServiceImpl implements ExplanationPointService {

    @Autowired
    private ExplanationPointDao explanationPointDao;

    @Autowired
    private AttDao attDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addExplanationPoint(ExplanationPoint explanationPoint) {
        if(explanationPoint.getAttachment() != null && explanationPoint.getAttachment().size() == 1){
            explanationPoint.setUseAtt(explanationPoint.getAttachment().get(0).getDldLink());
        }
        boolean flag = explanationPointDao.insertExplanationPoint(explanationPoint) != 0;
        if(explanationPoint.getAttachment() != null && explanationPoint.getAttachment().size()>0){
            attDao.deleteDataByDataNo(String.valueOf(explanationPoint.getId()), BizTypeEnum.Em_ExplanationPoint.getValue());
            for (Att att : explanationPoint.getAttachment()) {
                att.setBizType(BizTypeEnum.Em_ExplanationPoint.getValue());
                att.setDataNo(Integer.valueOf(explanationPoint.getId()));
                att.setcUser(explanationPoint.getCreateUser());
                att.setAddType(getExtName(att.getAttName()));
                attDao.insertData(att);
            }
        }
        return  flag;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean editExplanationPoint(ExplanationPoint explanationPoint) {
        boolean flag = explanationPointDao.updateExplanationPoint(explanationPoint) != 0;
        if(explanationPoint.getAttachment() != null && explanationPoint.getAttachment().size()>0){
            attDao.deleteDataByDataNo(String.valueOf(explanationPoint.getId()), BizTypeEnum.Em_ExplanationPoint.getValue());
            for (Att att : explanationPoint.getAttachment()) {
                att.setBizType(BizTypeEnum.Em_ExplanationPoint.getValue());
                att.setDataNo(Integer.valueOf(explanationPoint.getId()));
                att.setcUser(explanationPoint.getUpdateUser());
                att.setAddType(getExtName(att.getAttName()));
                attDao.insertData(att);
            }
        }
        return flag;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveExplanationPoint(ExplanationPoint explanationPoint) {
        if (ObjectUtil.isNotNull(explanationPoint.getId()) && !"".equals(explanationPoint.getId())){
            explanationPoint.setUpdateUser(explanationPoint.getCreateUser());
            return editExplanationPoint(explanationPoint);
        }
            return addExplanationPoint(explanationPoint);
    }

    @Override
    public boolean removeExplanationPoint(ExplanationPoint explanationPoint) {
        return explanationPointDao.deleteExplanationPoint(explanationPoint.getId()) != 0;
    }

    @Override
    public List<ExplanationPoint> getExplanationPointList(ExplanationPointDto explanationPointDto) {
        List<ExplanationPoint> explanationPointList = explanationPointDao.selectExplanationPointList(explanationPointDto);
        Att att = null;
        for (ExplanationPoint e : explanationPointList){
            att = new Att();
            att.setBizType(BizTypeEnum.Em_ExplanationPoint.getValue());
            att.setDataNo(Integer.valueOf(e.getId()));
            att.setState(1);
            e.setAttachment(attDao.queryDataList(att));
        }
        return explanationPointList;
    }

    @Override
    public ExplanationPoint getExplanationPointById(ExplanationPoint explanationPoint) {
        ExplanationPoint result = explanationPointDao.selectExplanationPointById(explanationPoint.getId());
        Att att = new Att();
        att.setBizType(BizTypeEnum.Em_ExplanationPoint.getValue());
        att.setDataNo(Integer.valueOf(result.getId()));
        att.setState(1);
        result.setAttachment(attDao.queryDataList(att));
        return result;
    }

    @Override
    public PageInfo<ExplanationPoint> getExplanationPointPage(ExplanationPointDto explanationPointDto) {
        PageHelper.startPage(explanationPointDto.getPage(),explanationPointDto.getRows());
        List<ExplanationPoint> list = explanationPointDao.selectExplanationPointList(explanationPointDto);
        Att att = null;
        for (ExplanationPoint e : list){
            att = new Att();
            att.setBizType(BizTypeEnum.Em_ExplanationPoint.getValue());
            att.setDataNo(Integer.valueOf(e.getId()));
            att.setState(1);
            e.setAttachment(attDao.queryDataList(att));
        }
        PageInfo<ExplanationPoint> pageInfo = new PageInfo<>(list, explanationPointDto.getRows());
        return pageInfo;
    }

    @Override
    public List<ExplanationLine> getExplanationPointOnLine(ExplanationPoint explanationPoint) {
        return explanationPointDao.selectExplanationPointOnLine(explanationPoint.getId());
    }

    private String getExtName(String fileName) {
        int i = fileName.lastIndexOf('.');
        int leg = fileName.length();
        return i > 0 ? (i + 1) == leg ? " " : fileName.substring(i + 1) : " ";
    }
}
